################################################################################
##
## Purpose: This script creates SI Figure 6
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/SI_figure_6.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(ggrepel)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()



load('./data/prepped/finalData.RData')

toplot2 <- utterance_level %>%
  arrange(docID,ind) %>%
  mutate(interruptor2 = ifelse(interruptor == 1 & lag(interrupted,2) == 0,1,0)) %>%
  mutate(fed = ifelse(fed == 0 & GOP == 1 & Male == 1,'GOP - Male',
                      ifelse(fed == 0 & GOP == 1 & Male == 0,'GOP - Female',
                             ifelse(fed == 0 & DEM == 1 & Male == 1,'DEM - Male',
                                    ifelse(fed == 0 & DEM == 1 & Male == 0,'DEM - Female',
                                           ifelse(fed == 0 & GOP == 0 & DEM == 0,'Expert','Fed Chair')))))) %>%
  filter(fed != 'Expert') %>%
  mutate(fed = ifelse(grepl('Male',fed),'Male',
                      ifelse(grepl('Female',fed),'Female',fed))) %>%
  group_by(opensecretsID,fed) %>%
  summarise(propInterrupted = mean(interrupted),
            propInterruptor = mean(interruptor),
            propInterruptor2 = mean(interruptor2),
            n = n()) %>%
  ungroup() %>%
  mutate(mult = propInterrupted / propInterruptor,
         mult2 = propInterrupted / propInterruptor2) %>%
  filter(n > 20)  %>%
  select(-matches('mult')) %>%
  pivot_longer(cols = c('propInterruptor','propInterruptor2')) %>%
  mutate(mult = propInterrupted/value)


pdf('./output/figures/SI_figure_6.pdf',width = 7,height = 5)
toplot2 %>%
  filter(name == 'propInterruptor2') %>%
  ggplot(aes(x = propInterrupted,y = value,size = n,color = fed == 'Fed Chair')) + 
  geom_point() + 
  scale_color_manual(name = 'Speaker',values = c('grey60','black')) +  
  geom_abline(slope = 1,intercept = 0) + 
  geom_text_repel(data = toplot2 %>% filter(grepl('FED',opensecretsID),
                                            name == 'propInterruptor2'),aes(label = paste0(gsub('FED','',opensecretsID),' (x',
                                                                                           round(mult,1),')')),
                  size = 3,color = 'black',min.segment.length = 0,point.padding = .5,box.padding = .5) + 
  scale_x_continuous(labels = scales::percent,limits = c(0,.3)) + 
  scale_y_continuous(labels = scales::percent,limits = c(0,.3)) + 
  labs(title = 'Interrupted vs. Interrupting',
       size = '# Utterances',
       x = '% Interrupted',
       y = '% Interrupting') + 
  theme_bw()
dev.off()

# EOF